#include "stdafx.h"
#include "ColorAnimation.h"
#include "View.h"
#include "WidgetWin.h"

ColorAnimation::ColorAnimation(int max_step, int grain)
	: Animation(max_step, grain)
{
}


ColorAnimation::~ColorAnimation()
{
}

void ColorAnimation::AnimateColorTo(View* view, SkColor& old_color, SkColor& new_color)
{
	object_view_ = view;
	old_color_ = old_color;
	new_color_ = new_color;

	startAnimation();
}

SkColor ColorAnimation::getTempColor()
{
	return temp_color_;
}


bool ColorAnimation::step(int step_num)
{
	short r = old_color_.R() + (new_color_.R() - old_color_.R()) * (step_num + 1)/ getMaxStepNum();
	short g = old_color_.G() + (new_color_.G() - old_color_.G()) * (step_num + 1) / getMaxStepNum();
	short b = old_color_.B() + (new_color_.B() - old_color_.B()) * (step_num + 1) / getMaxStepNum();
	short a = old_color_.Alpha() + (new_color_.Alpha() - old_color_.Alpha()) * (step_num + 1) / getMaxStepNum();

	SkColor tempColor(r, g, b, a);
	temp_color_ = tempColor;

	RECT rc;
	rc.left = object_view_->X();
	rc.top = object_view_->Y();
	rc.right = object_view_->X() + object_view_->Width();
	rc.bottom = object_view_->Y() + object_view_->Height();
	::InvalidateRect(WidgetWin::getInstance().getNativeHwnd(), &rc, false);

	// ATLTRACE("[%s][%d][%d][%d]\r\n", __FUNCTION__, __LINE__, step_num, Time::now());

	return true;
}
